/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package doan2database;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author DongVan
 */
public class HoaDon {
    private int m_IDHoaDon;
	private int m_IDKhachHang;
	private String m_NgayLap;
	private String m_TongTien;
	public int getM_IDHoaDon() {
		return m_IDHoaDon;
	}
	public void setM_IDHoaDon(int m_IDHoaDon) {
		this.m_IDHoaDon = m_IDHoaDon;
	}
	public int getM_IDKhachHang() {
		return m_IDKhachHang;
	}
	public void setM_IDKhachHang(int m_IDKhachHang) {
		this.m_IDKhachHang = m_IDKhachHang;
	}
	public String getM_NgayLap() {
		return m_NgayLap;
	}
	public void setM_NgayLap(String m_NgayLap) {
		this.m_NgayLap = m_NgayLap;
	}
	public String getM_TongTien() {
		return m_TongTien;
	}
	public void setM_TongTien(String m_TongTien) {
		this.m_TongTien = m_TongTien;
	}
	
	public HoaDon(int m_IDHoaDon, int m_IDKhachHang, String m_NgayLap,
			String m_TongTien) {
		this.m_IDHoaDon = m_IDHoaDon;
		this.m_IDKhachHang = m_IDKhachHang;
		this.m_NgayLap = m_NgayLap;
		this.m_TongTien = m_TongTien;
	}
	
	public HoaDon(){
		
	}
	
	AESEncrypt aes = new AESEncrypt();
	Datapro dataprovider = new Datapro();
	
	public ArrayList<HoaDon> selected() {
		ArrayList<HoaDon> listHoaDon = new ArrayList<HoaDon>();
		String sqlQuery = "select * from HOA_DON";
		ResultSet rs = dataprovider.Selected(sqlQuery);
		try {
			while (rs.next()) {
				HoaDon itemHoaDon = new HoaDon();
				itemHoaDon.m_IDHoaDon = rs.getInt(1);
				itemHoaDon.m_IDKhachHang = rs.getInt(2);
				itemHoaDon.m_NgayLap = aes.decryptString(rs.getString(3));
				itemHoaDon.m_TongTien = aes.decryptString(rs.getString(4));
				listHoaDon.add(itemHoaDon);
			}
			dataprovider.disconnect();
	        rs.close();
	        return listHoaDon;
	        
	    } catch ( SQLException ex) {
	        Logger.getLogger(Datapro.class.getName()).log(Level.SEVERE, null, ex);
	    }
		return null;
	}
	

	public boolean insert() {
		int idKhachHang = this.m_IDKhachHang;
		String ngayLap = aes.encryptString(this.m_NgayLap);
		String tongTien = aes.encryptString(this.m_TongTien);
		String query =  "insert into HOA_DON values("+idKhachHang+",'"+ ngayLap + "','"+tongTien+"')";
		boolean result =  dataprovider.Insert(query);
		dataprovider.disconnect();
		return result;
	}
	
	public boolean update(){
		int idKhachHang = this.m_IDKhachHang;
		String ngayLap = aes.encryptString(this.m_NgayLap);
		String tongTien = aes.encryptString(this.m_TongTien);
		String query = "update HOA_DON set IDKhachHang = "+ idKhachHang +", NgayLap = '"+ngayLap+"', TinhTrang = '"+tongTien+"' where IDLoaiND = " + this.m_IDHoaDon + "\"";
		boolean result = dataprovider.Update(query);
		dataprovider.disconnect();
		return result;
	}
	
	public boolean delete(){
		String query = "delete from HOA_DON where = " + this.m_IDHoaDon + "\"";
		boolean result = dataprovider.Delete(query);
		dataprovider.disconnect();
		return result;
	}
}
